<html>
<head>
<title>unit.js Unit Test</title>
<script SRC="unit.js"></script>
<script>
function Sample()
{
	this.x = 1;
}

Sample.prototype.Double = function()
{
	this.x *= 2;
}

</SCRIPT>
</HEAD>
<BODY>
<h1><script>document.write(document.title);</script></h1>
Note: Be sure pop-ups are not blocked while running this test.
<SCRIPT>
ts = new UT.TestSuite();
ts.DWOutputDiv();
</script>
<script>
ts.AddTest("Single Failure", function(ut)
{
	ut.Assert(true, "true is true");
	ut.Assert(false, "this one should fail!");
}).Expect(1,2);

ts.AddTest("Never Run", function(ut)
{
	ut.Assert(false, "Disable a failing test.");
}).Enable(false).Expect(1,1).Reference("http://mckoss.com");

ts.AddTest("All Pass", function(ut)
{
	var s = new Sample();
	ut.Assert(s.x == 1, "Constructor");
	s.Double();
	ut.Assert(s.x == 2, "Double Test 1");
	s.x = 10;
	s.Double();
	ut.Assert(s.x == 20, "Double Test 2");
	ut.AssertEval("1+1==2");
	ut.AssertGT(2, 1);
	ut.AssertGT("Z", "A");
}).Expect(0,6);

ts.AddTest("All Fail", function(ut)
{
	ut.Assert(false);
	ut.AssertEval("1+1 == 3");
	ut.AssertEq(1, 2);
	ut.AssertNEq("hello", "hello");
	ut.AssertGT(1, 2);
	ut.AssertGT(undefined, 2);
	ut.AssertGT("A", "Z");
	var x = 7;
	ut.AssertFn(function () { return x != 7; });
	ut.AssertFn(new Function);
	ut.AssertFn(function () { });
	IllegalFunction();
}).Expect(11,11);

ts.AddTest("Object Comparison", function(ut)
{
	var obj1 = {a: 1, b: 2};
	var obj2 = {a: 1, b: 2};
	var obj3 = {a: 1};
        var obj4 = {a: 1, b: 3};
        var obj5 = {a: 1, b:{c:2, d: 3}};
        var obj6 = {a: 1, b:{c:2, d: 3}};
	
	ut.AssertEq(obj1, obj2);
	ut.AssertEq(obj1, obj3);	// Fails
        ut.AssertEq(obj1, obj4);        // Fails
        ut.AssertContains(obj1, obj3);
	ut.AssertIdent(obj1, obj2); // Fails
        ut.AssertEq(ut.PropCount(obj1), 2);
        ut.AssertEq(obj5, obj6);
}).Expect(3,16);

ts.AddTest("Types", function(ut)
{
	ut.AssertTypes({st:'hi', n:1, m:1.1, f:true, a:[1,2], o: {a:1}},
		{st:'string', n:'number', m:'number', f:'boolean', a:'array', o:'object'});
	ut.AssertType([1,2], Array);
	var f1 = new Foo();
	ut.AssertType(f1, Foo);
	ut.AssertType(f1, 'object');
	
	function Foo(){}
}).Expect(0, 12);

// This test is complete after 7 seconds!
var utNA = ts.AddTest("No Asserts Asynch", function(ut)
{
}).Async(true);

ts.AddTest("Async timeout", function(ut)
{
	// No call to Async(false)
}).Async(true, 100).Expect(1,1);

ts.AddTest("Multiple Async calls", function(ut)
{
	new UT.Timer(function () {ut.Async(false);}, 1000).Active(true);
	new UT.Timer(function () {ut.Async(false);}, 1000).Active(true);
	new UT.Timer(function () {ut.Async(false);}, 1000).Active(true);
}).Async(3);

ts.AddTest("AsyncSequence", function(ut)
{
	var tm = new UT.Timer(function () {tm.cAS++; ut.NextFn()}, 1000);
	tm.cAS = 0;
	ut.AsyncSequence(
		[
		function (utT) {
			ut.Assert(utT == ut);
			ut.Assert(tm.cAS == 0);
			tm.Active(true);
		},

		function (utT) {
			ut.Assert(utT == ut);
			ut.Assert(tm.cAS == 1);
			tm.Active(true);
		},

		function (utT) {
			ut.Assert(utT == ut);
			ut.Assert(tm.cAS == 2);
			tm.Active(true);
		}
		]);
}).Async(true);

ts.AddTest("Script error expected", function(ut)
{
	IllegalFunction();
}).Throws(UT.Browser.fIE ? "TypeError" : "ReferenceError").Expect(0,1);

ts.AddTest("Some exception expected", function(ut)
{
	IllegalFunction();
}).Throws().Expect(0,1);

ts.AddTest("no exception", function(ut)
{
	ut.Assert(true);
}).Throws().Expect(1,2);

ts.AddTest("Fails: Wrong exception", function(ut)
{
	throw new Error("the wrong exception");
}).Throws("ReferenceError").Expect(1,1);

ts.AddTest("Multiple Exceptions", function(ut)
{
	try { IllegalFunction(); }
	catch (e) { ut.AssertException(e, UT.Browser.fIE ? "TypeError" : "ReferenceError"); }
	
	ut.AssertThrows(undefined, function(ut1)
		{
		ut.AssertIdent(ut, ut1);
		throw new Error("Custom exception");
		});

	ut.AssertThrows("Custom", function (ut)
		{
		throw new Error("Custom exception");
		});
}).Expect(0,4);

ts.AddTest("Markers and Assert Numbers", function(ut)
{
	ut.BreakOn(8);
	for (var i = 0; i < 10; i++)
		{
		ut.Trace("Trace Name " + i);
		ut.Assert(i != 7, "Assert Fails on 7");
		}
}).Expect(1,10);

ts.AddSubTest("passes.html").Expect(0,1);
ts.AddSubTest("fails.html").Expect(1,1);

ts.Run();
ts.AddTest("Post Run Test", function(ut) {ut.Assert(true);}).Expect(0,1);
ts.Report();
window.setInterval(function() { utNA.Async(false); }, 7000);

</script>
</body>
</html>

